package znbkSameQuesTranslate.SQ_TLXZ;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.znbk.znbklibrary.base.BaseRecyclerAdapter;
import com.example.znbk.znbklibrary.base.BaseRecyclerViewHolder;
import com.example.znbk.znbklibrary.util.LancooUtils;
import com.example.znbk.znbklibrary.widget.CustomTextViewToPicture;
import com.example.znbk.znbklibrary.widget.ScaleButton;
import com.lancoo.znbkxx.R;

import java.util.List;

import znbkSameQuesTranslate.GetSameQuesJson.SameQuesPaperEntity;

public class SQxlTlxzAnswerAdapter extends BaseRecyclerAdapter {


    private Context context;
    private List<SameQuesPaperEntity> sameQuesPaperEntity;
    private int quesIndex = -1;
    private int childIndex= -1;
    private CallBackAdapterTlxz callBackAdapterTlxz;
    private LayoutInflater inflater;

    public SQxlTlxzAnswerAdapter(Context context, List<SameQuesPaperEntity> sameQuesPaperEntity, int quesIndex, int childIndex, CallBackAdapterTlxz callBackAdapterTlxz){
        this. context=context;
        this.sameQuesPaperEntity = sameQuesPaperEntity;
        this.quesIndex = quesIndex;
        this.childIndex = childIndex;
        this.callBackAdapterTlxz = callBackAdapterTlxz;
        inflater=LayoutInflater. from(context);
        getAnswerInfor();
    }

    private int subChildOptionCount = -1;
    private int SortIndex = -1;
    private LinearLayout mLlOption;
    private String SubChildAsk = "";
    private int ChildListSize  =0;
    private void getAnswerInfor(){

        int SubmitState = sameQuesPaperEntity.get(quesIndex).getFavouriteAndNote().getSubmitState();
        int Size = sameQuesPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().size();
        if(SubmitState==0) {
            for (int i = 0; i < Size; i++) {
                sameQuesPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().get(i).
                        getSubChildList().get(0).getSubChildItem().get(0).setStuAnswer("");
            }
        }

        subChildOptionCount = sameQuesPaperEntity.get(quesIndex).getQuesFromTK().getChildList().
                get(childIndex).getSubChildList().get(0).getOptionList().size();
        SortIndex = sameQuesPaperEntity.get(quesIndex).getQuesFromTK().getChildList().
                get(childIndex).getSubChildList().get(0).getSubChildItem().get(0).getSortIndex();
        SubChildAsk = sameQuesPaperEntity.get(quesIndex).getQuesFromTK().getChildList().
                get(childIndex).getSubChildList().get(0).getSubChildAsk();
        ChildListSize = sameQuesPaperEntity.get(quesIndex).getQuesFromTK().getChildList().size();
    }

    @Override
    public int getItemCount() {
        return subChildOptionCount;
    }

    private TextView mTvChildQuesAsk;
    private TextView mTvChildPageInfor;
    private int choosepos = -1;
    private ScaleButton mSBtnOptionIndex;
    private TextView mTvOptionContent;

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder baseRecyclerViewHolder, final int position) {
        mLlOption = baseRecyclerViewHolder.getView(R.id.ll_option);
        Log.e("onBindViewHolder","====AAAA=============终于进来了====");
        mTvChildQuesAsk = baseRecyclerViewHolder.getView(R.id.tv_childQuesAsk);
        mTvChildPageInfor = baseRecyclerViewHolder.getView(R.id.tv_childPageInfor);
        if(position==0){
            mTvChildQuesAsk.setVisibility(View.VISIBLE);
            new CustomTextViewToPicture(context,mTvChildQuesAsk,SortIndex+"、 "+SubChildAsk);

            int childPageCount = sameQuesPaperEntity.get(quesIndex).getQuesFromTK().getTotalItemCount();
            mTvChildPageInfor.setText(Html.fromHtml(context.getResources().getString(R.string.child_paper_infor,(childIndex+1),childPageCount)));
        }else{
            mTvChildQuesAsk.setVisibility(View.GONE);
            mTvChildPageInfor.setVisibility(View.GONE);
        }

        mSBtnOptionIndex = baseRecyclerViewHolder.getView(R.id.sbtn_optionIndex);
        String OptionIndex = sameQuesPaperEntity.get(quesIndex).getQuesFromTK().getChildList().
                get(childIndex).getSubChildList().get(0).getOptionList().get(position).getOption();
        mSBtnOptionIndex.setText(OptionIndex);
        mSBtnOptionIndex.setTag(position);
        mSBtnOptionIndex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int SubmitState = sameQuesPaperEntity.get(quesIndex).getFavouriteAndNote().getSubmitState();
                if(SubmitState==0||SubmitState==2){
                    return;
                }
                doRefresh(position);
            }
        });

        mTvOptionContent = baseRecyclerViewHolder.getView(R.id.tv_optionContent);
        String OptionContent = sameQuesPaperEntity.get(quesIndex).getQuesFromTK().getChildList().
                get(childIndex).getSubChildList().get(0).getOptionList().get(position).getContent();
        new CustomTextViewToPicture(context,mTvOptionContent,OptionContent);
        mTvOptionContent.setTag(position);
        mTvOptionContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int SubmitState = sameQuesPaperEntity.get(quesIndex).getFavouriteAndNote().getSubmitState();
                if(SubmitState==0||SubmitState==2){
                    return;
                }
                doRefresh(position);
            }
        });

        String StuAnswer = sameQuesPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                get(childIndex).getSubChildList().get(0).getSubChildItem().get(0).getStuAnswer();
        if(StuAnswer!=null) {
            if(StuAnswer.equals("")){
                choosepos = -1;
            }else {
                choosepos = Integer.valueOf(LancooUtils.stringToAscii(StuAnswer)) - 65;
            }
        }else{
            choosepos = -1;
        }
        if (choosepos == position){

            int SubmitState = sameQuesPaperEntity.get(quesIndex).getFavouriteAndNote().getSubmitState();
            String SubChildStuAnswer = sameQuesPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                    get(childIndex).getSubChildList().get(0).getSubChildItem().get(0).getStuAnswer();
            String SubChildAnswer = sameQuesPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                    get(childIndex).getSubChildList().get(0).getSubChildItem().get(0).getItemAnswer();
            if(SubmitState==2) {

                if (SubChildAnswer.equals(SubChildStuAnswer)) {
                    mSBtnOptionIndex.setTextColor(context.getResources().getColor(R.color.lancoooWhite));
                    mSBtnOptionIndex.setBackgroundResource(R.drawable.round_green);
                    mTvOptionContent.setTextColor(context.getResources().getColor(R.color.lancoooGreen));
                }else{
                    mSBtnOptionIndex.setTextColor(context.getResources().getColor(R.color.lancoooWhite));
                    mSBtnOptionIndex.setBackgroundResource(R.drawable.round_red);
                    mTvOptionContent.setTextColor(context.getResources().getColor(R.color.lancoooRed));
                }
            }else {
                mSBtnOptionIndex.setTextColor(context.getResources().getColor(R.color.lancoooWhite));
                mSBtnOptionIndex.setBackgroundResource(R.drawable.round_blue);
                mTvOptionContent.setTextColor(context.getResources().getColor(R.color.lancoooBlue));
            }

        }else {
            int SubmitState =sameQuesPaperEntity.get(quesIndex).getFavouriteAndNote().getSubmitState();
            String ItemAnswer = sameQuesPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                    get(childIndex).getSubChildList().get(0).getSubChildItem().get(0).getItemAnswer();
            if(SubmitState==2) {
                int TrueAnswerAscii = Integer.valueOf(LancooUtils.stringToAscii(ItemAnswer)) - 65;
                if(TrueAnswerAscii==position){
                    mSBtnOptionIndex.setTextColor(context.getResources().getColor(R.color.lancoooWhite));
                    mSBtnOptionIndex.setBackgroundResource(R.drawable.round_green);
                    mTvOptionContent.setTextColor(context.getResources().getColor(R.color.lancoooGreen));
                }else{
                    mSBtnOptionIndex.setTextColor(context.getResources().getColor(R.color.lancoooBlack));
                    mSBtnOptionIndex.setBackgroundResource(R.drawable.round_white);
                    mTvOptionContent.setTextColor(context.getResources().getColor(R.color.lancoooBlack));
                }
            }else {
                mSBtnOptionIndex.setTextColor(context.getResources().getColor(R.color.lancoooBlack));
                mSBtnOptionIndex.setBackgroundResource(R.drawable.round_white);
                mTvOptionContent.setTextColor(context.getResources().getColor(R.color.lancoooBlack));
            }
        }

        if(position==subChildOptionCount-1){
            mLlOption.removeAllViews();
            View mView = new View(context);
            mView.setBackgroundColor(Color.parseColor("#ffffff"));
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,300);
            mLlOption.addView(mView,lp);
        }

    }

    @Override
    public View getHolderView(ViewGroup parent, int position) {
        View view = inflater.inflate(R.layout.sq_adapter_item_answer_tlxz,parent, false);
        return view;
    }

    public void doRefresh(int position){
        choosepos = position;
        //输入学生作答
        if(position!=-1) {
            sameQuesPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().get(childIndex).
                    getSubChildList().get(0).getSubChildItem().get(0).setStuAnswer(LancooUtils.asciiToString(String.valueOf(65 + position)));
        }

        int AnsweredCount=0;
        for(int i=0;i<ChildListSize;i++){
            String StuAnswer = sameQuesPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                    get(i).getSubChildList().get(0).getSubChildItem().get(0).getStuAnswer();
            if(!StuAnswer.equals("")){
                AnsweredCount = AnsweredCount+1;
            }
        }
        //若作答完成则显示答案，记录作答状态，否则亦然
        if (AnsweredCount == ChildListSize) {
            sameQuesPaperEntity.get(quesIndex).getFavouriteAndNote().setSubmitState(2);
            callBackAdapterTlxz.callAnsweredAll();
        }else{
            sameQuesPaperEntity.get(quesIndex).getFavouriteAndNote().setSubmitState(1);
        }

        notifyDataSetChanged();
    }

    public interface CallBackAdapterTlxz {
        void callAnsweredAll();
    }

}